function spider_list(url, cellback) {
    const reptileUrl = url
    superagent.get(reptileUrl).end(function (err, res) {
        // 抛错拦截
        if (err) {
            console.log('false')
        }
        // 等待 code
        let $ = cheerio.load(res.text)
        //var cont = $('#mainScreen .container .web-blog .row .blog-list-wrap .tab article-list')
        var cont = $('.main_ .container .row .col-xs-12').children()
        //console.log(cont)
        var list = []
        cont.each(function (i, elem) {
            list.push({
                text: $(elem).text(),
                href: $(elem).attr('href') || '123'
            })

        })
        cellback(list)
    })
}

function spider_name_list(url, cellback) {
    const reptileUrl = url
    superagent.get(reptileUrl).end(function (err, res) {
        // 抛错拦截
        if (err) {
            console.log('false')
        }
        if (res.text) {
            return;
        }
        // 等待 code
        let $ = cheerio.load(res.text)
        //var cont = $('#mainScreen .container .web-blog .row .blog-list-wrap .tab article-list')
        var cont = $('.container .row .col-xs-12').children()
        //console.log(cont)
        var list = []
        cont.each(function (i, elem) {
            list.push({
                text: $(elem).text() || '测试姓名',
                href: $(elem).attr('href') || '123'
            })

        })
        cellback(list)
    })
}

app.get('/api/v3/getData', function (req, res) {
    spider_list('http://www.resgain.net/xsdq.html', function (data) {
        res.send(data)
    })
})

function getName_func(cellback) {

}
app.get('/api/v3/getNameList', function (req, res) {
    var big_arr = []
    for (var x = 1; x < 100; x++) {
        if (x == 1) {
            spider_name_list('http://zhao.resgain.net/name/boys.html', function (data) {
                console.log(data)
                big_arr.push(data)
            })
        } else {
            spider_name_list('http://zhao.resgain.net/name/boys_' + x + '.html', function (data) {
                console.log(data)
                if (data == '') {
                    return;
                }
                big_arr.push(data)
            })
        }
    }
    setTimeout(() => {
        res.send(big_arr)
    }, 30000);
})

app.get('/api/v1/getNameCont', function (req, res) {
    var xing = req.query.xing
    var sex = req.query.sex
    var reptileUrl = 'http://' + xing + '.resgain.net/name/' + sex
    name_cont(reptileUrl,function(data){
        res.send(data)
    })
})

function name_cont(reptileUrl, cellback) {
    var list = []
    for (var x = 1; x < 11; x++) {
        req_url = reptileUrl + '_' + x + '.html'
        superagent.get(req_url).end(function (err, res) {
            // 抛错拦截
            if (err) {
                console.log('false')
            }
            // 等待 code
            let $ = cheerio.load(res.text)
            var cont = $('.container').eq(4).find('.row .col-xs-12').children()
            cont.each(function (i, elem) {
                list.push($(elem).text())
            })
        })
    }
    setTimeout(function(){
        cellback(list)
    },5000)
}





